<?

class compile_static_task extends system_task
{
	private $groups, $md5, $force;

	public function execute( array $params = array() )
	{
		$this->out('Gathering static files...');
		
		$this->force = in_array('force', $params);

		$this->gather_files( PHLIGHT_PATH );
		$this->gather_files( PROJECT_PATH );
		$this->build();
	}

	private function gather_files( $dir )
	{
		$i = new RecursiveIteratorIterator(
				new RecursiveDirectoryIterator($dir),
				RecursiveIteratorIterator::SELF_FIRST
			);

		foreach ( $i as $file ) $files[] = (string)$file;
		asort($files);

		foreach ($files as $file)
		{
			if ( strpos($file, '.svn') ) continue;
			if ( strpos($file, 'web/public') ) continue;
			if ( strpos($file, 'generate/plugins') ) continue;

			if ( config::get('static_exclude') ) foreach ( config::get('static_exclude') as $path )
				if ( strpos($file, $path) ) continue 2;
			
			$ext = pathinfo($file, PATHINFO_EXTENSION);

			if ( $ext == 'css' )
			{
				if ( strpos($file, 'ie.css') ) $group = 'ie';
				else if ( strpos($file, 'print.css') ) $group = 'print';
				else $group = 'screen';

				$this->groups[$group . '.css'][] = (string)$file;
				$this->md5[$group . '.css'] .= md5_file($file);

				$this->out("Found: {$file}");
			}
			else if ( $ext == 'js' )
			{
				if ( strpos($file, 'ie.js') ) $group = 'ie';
				else if ( strpos($file, 'debug.js') ) $group = 'debug';
				else if ( strpos($file, '.nc.js') ) continue;
				else $group = 'system';

				$this->groups[$group . '.js'][] = (string)$file;
				$this->md5[$group . '.js'] .= md5_file($file);
				$this->out("Found: {$file}");
			}
		}
	}

	public function build()
	{
		$this->out('Building files...');

		$min_allowDebugFlag = false;
		$min_errorLogger = false;
		$min_enableBuilder = false;
		$min_documentRoot = '';
		$min_serveOptions['encodeOutput'] = false;
		$min_serveOptions['bubbleCssImports'] = false;
		$min_serveOptions['minApp']['groupsOnly'] = true;
		$min_libPath = SYSTEM_ROOT . '/external/minify';

		set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
		require 'Minify.php';

		$_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;

		$min_serveOptions['minApp']['groups'] = $this->groups;

		error_reporting(E_NONE);
		$hash = $this->get_hash();
		
		foreach ( $min_serveOptions['minApp']['groups'] as $group => $files )
		{
			$this->md5[$group] = md5($this->md5[$group]);
			
			if ( ( $this->md5[$group] != $hash[$group] ) || array_key_exists('SHELL', $_SERVER) )
			{
				$_GET['g'] = $group;
				
				ob_start();
				Minify::serve('MinApp', $min_serveOptions);

				$content = ob_get_clean();
				$content = $this->compile($group, $content) ;
				$this->store_file($group, $content);
			}
		}

		$this->store_hash( $this->md5 );

		error_reporting(E_ALL & ~E_NOTICE);
	}

	public function store_file($name, $content)
	{
		$web_path = PROJECT_PATH . '/apps/main/web/public';
		$this->out("Storing {$name}");

		$path = $web_path . '/' . $name;
		file_put_contents($path, $content);
		chmod($path, 0777);
	}

	public function get_hash()
	{
		return include PROJECT_PATH . '/apps/main/web/public/hashes.php';
	}

	public function store_hash($hash)
	{
		$web_path = PROJECT_PATH . '/apps/main/web/public';
		$this->out("Storing hashes");

		$path = $web_path . '/hashes.php';

		$content = '<? return array(';
		foreach ( $hash as $group => $md5 ) $content .= "'{$group}' => '{$md5}',";
		$content .= ');';

		exec('rm -rf ' . $path);
		file_put_contents($path, $content);
		chmod($path, 0777);
	}
	
	public static function compile( $name, $data )
	{
		$ext = pathinfo($name, PATHINFO_EXTENSION);
		
		switch ( $ext )
		{
			case 'css':
				$data = static_compiler::css($data);
				break;
			
			case 'js':
				$data = static_compiler::js($data);
				break;
		}
		
		return $data;
	}
}